package com.icoolkj.learn.juc.future;

import com.icoolkj.learn.juc.base.utils.SleepTools;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/03/05 14:15
 */
@Slf4j
public class CFDemo {
    public static void main(String[] args) {
        Runnable runnable = () -> { log.info("执行无返回结果的异步任务");};

        // 提交一个异步任务
        CompletableFuture.runAsync(runnable);

        SleepTools.second(1);

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            log.info("执行有返回值的异步任务");
            SleepTools.second(5);
            return "hello icoolkj";
        });
        try {
            String result = future.get();
            log.info("任务结果： {}", result);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }


        SleepTools.second(1);

    }
}
